#!/usr/bin/sh

light=$SIS_SH/sis-light

key=$1
# default="get"

# ARGUMENTS FOR HELP:
if [[ "" == $key ]] ; then
  echo "Error: empty param"
  echo "for more info run: help-light"
  
  bash $SIS_SH/help-light

  # key=$default # - default # or:
  exit `false` # close this with error
elif [[ "--help" == $key || "--version" == $key ]] ; then
  bash $SIS_SH/help-light $key
    
  exit `true` # close  - becose this help-run
fi

num=$2

# not use files but use this script like a config for sed
min=30
max=70
step=5

off=0
null=1 # recomended not full off to null
full=100

if [[ "check" == $key ]] ; then
  if [[ "min" != $num && "max" != $num && "step" != $num && "off" != $num && "null" != $num && "full" != $num ]] ; then
    echo "Command not defined!"
  	exit `false`
  fi

elif [[ "get" == $key ]] ; then
  if [[ "" == $num ]] ; then
      # $SIS_SH/get-light
      exit `$SIS_BIN/get-light-from`
  else 
    $light check $num
	if [[ $? == 0 ]] ; then
	  echo $(( $num ))
    else
      exit `false`
	fi
  fi

elif [[ "set" == $key ]] ; then
  command=$(( $num ))

  # find command:
  $light check $num
  
  # if command not found then: exit with Error
  if [[ $? == 1 ]] ; then
    exit `false`
  fi
  $light = $command

elif [[ "+=" == $key ]] ; then
echo 1
  $light + $step
elif [[ "-=" == $key ]] ; then
  $light - $step

elif [[ "+" == $key ]] ; then
  sum=$(( `$light get` + $num ))
  $light = $sum
elif [[ "-" == $key ]] ; then
  sum=$(( `$light get` - $num )) 
  $light = $sum

elif [[ "=" == $key ]] ; then
  # num dont more END and dont less NULL
  if [[ $(( num > full )) == 1 ]] ; then
    num=$full
  elif [[ $(( num < null )) == 1 ]] ; then
    num=$null
  fi

  # light set to num
  light -S $num

elif [[ "off" == $key ]] ; then
  light -S $off
elif [[ "mute" == $key ]] ; then
  $SIS_SH/sis-light-mute

elif [[ "save" == $key ]] ; then
	echo `$light get` > ~/.config/sis/save/light.save
elif [[ "load" == $key ]] ; then
	# $volume set number `cat ~/.config/sis/volume/save`
	$light = `cat ~/.config/sis/save/light.save`
else
     exit `false`
fi

